package com.codejob.portal.web;

import java.util.Enumeration;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.codejob.portal.entity.user.Users;
import com.codejob.portal.web.utils.Struts2Utils;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial") 
public abstract class BaseAction extends ActionSupport  {

	protected Logger logger = LoggerFactory.getLogger(getClass());

    protected boolean isLogin(){
    	Users user=(Users)Struts2Utils.getSession().getAttribute("user");
    	if(user==null){
    		setUrl();
    		return false;
    	}
    	return true;
    }
    protected boolean isManager(){
    	Users user=(Users)Struts2Utils.getSession().getAttribute("user");
    	if(user==null||user.getIsManager()!=2){
    		setUrl();
    		return false;
    	}
    	return true;
    }
    
    protected Long getUserId(){
    	Users user=(Users)Struts2Utils.getSession().getAttribute("user");
    	return user.getId();
    }
    
    protected Users getUser(){
    	Users user=(Users)Struts2Utils.getSession().getAttribute("user");
    	return user;
    }
	protected void setUrl(){
		StringBuffer sbUrl=Struts2Utils.getRequest().getRequestURL();
		
		sbUrl.append("?").append("r=").append(System.currentTimeMillis());
		Enumeration names = Struts2Utils.getRequest().getParameterNames();
		while(names.hasMoreElements()){
			String name=(String)names.nextElement();
			String value=Struts2Utils.getRequest().getParameter(name);
			sbUrl.append("&").append(name).append("=").append(value);
		}
		//System.out.println(sbUrl.toString());
		Struts2Utils.getSession().setAttribute("unLoginUrl", sbUrl.toString()); 
	}
}
